package com.woniuxy.charin.runner;

import com.woniuxy.charin.cache.AuthCache;
import com.woniuxy.charin.common.RedisConst;
import com.woniuxy.charin.model.entity.Permission;
import com.woniuxy.charin.model.entity.RolePermission;
import com.woniuxy.charin.model.entity.UserRole;
import com.woniuxy.charin.service.PermissionService;
import com.woniuxy.charin.service.RolePermissionService;
import com.woniuxy.charin.service.UserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Component
public class CacheRunner implements CommandLineRunner {
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private RolePermissionService rolePermissionService;
    @Autowired
    private PermissionService permissionService;

    @Autowired
    RedisTemplate redisTemplate;


    @Override
    public void run(String... args) throws Exception {

        init();


    }
    public void init(){
        // 获取HashOperations对象以简化代码
        HashOperations<String, String, Object> hashOps = redisTemplate.opsForHash();

// 更新UserRole数据
        List<UserRole> userRoles = userRoleService.list(null);
        String userRoleKey = RedisConst.UserRole;
        if (redisTemplate.hasKey(userRoleKey)) {
            // 删除旧的数据
            redisTemplate.delete(userRoleKey);
        }
// 批量插入新的UserRole数据
        Map<String, UserRole> userRoleMap = userRoles.stream()
                .collect(Collectors.toMap(item -> String.valueOf(item.getId()), Function.identity()));
        hashOps.putAll(userRoleKey, userRoleMap);

// 更新RolePermission数据
        List<RolePermission> rolePermissions = rolePermissionService.list(null);
        String rolePermissionsKey = RedisConst.RolePermissions;
        if (redisTemplate.hasKey(rolePermissionsKey)) {
            // 删除旧的数据
            redisTemplate.delete(rolePermissionsKey);
        }
// 批量插入新的RolePermission数据
        Map<String, RolePermission> rolePermissionMap = rolePermissions.stream()
                .collect(Collectors.toMap(item -> String.valueOf(item.getId()), Function.identity()));
        hashOps.putAll(rolePermissionsKey, rolePermissionMap);

// 更新Permission数据
        List<Permission> permissions = permissionService.list(null);
        String permissionsKey = RedisConst.Permissions;
        if (redisTemplate.hasKey(permissionsKey)) {
            // 删除旧的数据
            redisTemplate.delete(permissionsKey);
        }
// 批量插入新的Permission数据
        Map<String, Permission> permissionMap = permissions.stream()
                .collect(Collectors.toMap(item -> String.valueOf(item.getId()), Function.identity()));
        hashOps.putAll(permissionsKey, permissionMap);
    }
}
